from math import *

# wire radius, see http://www.hardwarebook.info/AWG
wire = 0.255 # millimeter, diameter of wire, for 30 AWG, 0.051 mm^2 
N = 12  # divide a circle to N arcs
R = 5 # radius of base circle
dZ = 0.3 # turn displacement, bigger than wire diameter
dR = 0 # radius percent change per turn, normally, abs(R*dR) > wire
turn = 8  # number of turns

def gen_tail():
    return """
.freq fmin=10000 fmax=1e10 ndec=1
.end\n"""

def gen_header(turn, disZ, divider=6):
    return """*** coil with %s turns, %f mm distance\n\
.units mm

.default nwinc=%d nhinc=%d

"""%(turn,disZ,divider,divider)
    

def gen_coil(R,dZ,dR,turn, startNum, disZ, wire=0.255,N=12):
    """base (R)adius, dZ per turn, dR percent change, numTurn, node start number, Z position, [wire width, circle node number]"""
    outS = ""
    num = int(turn*N)+1
    for i in range(num):
        theta = 2.0*i*pi/N
        r = R * (1+1.0*i*dR/N)
        z = 1.0*dZ*i/N + disZ
        outS += "\nn%s x=%f y=%f z=%f"%(str(i+startNum), r*cos(theta), r*sin(theta),z)
        
    for i in range(num-1):
        outS += "\ne%s n%s n%s w=%f h=%f"%(str(i+startNum), str(i+startNum),str(startNum+i+1), wire,wire)

    outS += "\n.external n%s n%s"%(startNum,startNum+num-1)
    return (outS, num)
        
seccoil, lastnum = gen_coil(2.5,0,-1.0/16, 8, 0, 0)
startZ = 1.0
endZ = 100.0
numPoint = 10


for z in [startZ*(endZ/startZ)**(x*1.0/numPoint) for x in range(numPoint+1)]:
    fout = open("dis_%.2f.inp"%z,'w')
    fout.write(gen_header(8, z))
    fout.write(seccoil)
    fout.write(gen_coil(5,0.3,0,8, lastnum, z)[0])
    fout.write(gen_tail())
    fout.close()
    print "write to " "dis_%.2f.inp"%z

